package controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/3/14 17:13
 * 实现注销功能
 */
@WebServlet("/logout")
@SuppressWarnings({"all"})
public class LogoutServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if (session == null) {
            // 会话不存在, 还未登录就不需要注销
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("很抱歉, 您还未登录, 不需要注销~");
            return;
        }
        // 实现注销
        session.removeAttribute("user");
        resp.sendRedirect("blog_login.html");
    }
}
